//
//  CustomCameraShootPreview.m
//  MinesTime
//
//  Created by imac on 2017/11/8.
//  Copyright © 2017年 MS1968. All rights reserved.
//

#import "CustomCameraShootPreview.h"
#import "CameraHollowView.h"

typedef NS_ENUM(NSUInteger, CurrentGetImageType) {
    CurrentGetImageTypeAlbum,
    CurrentGetImageTypeCamera,
};

static NSString *takePhotoTipsString1 = @"手表对准到框内，保持光线充足";
static NSString *takePhotoTipsString2 = @"识别中 ...";
@interface CustomCameraShootPreview ()
/** 按钮组 0pop 1close 2album 3takePhoto */
@property (strong, nonatomic) IBOutletCollection(QMUIButton) NSArray *cameraPreviewButtonArr;
/** 当前获取图片方式 */
@property (nonatomic, assign) CurrentGetImageType currentGetImageType;

/** 导航栏提示语 */
@property (weak, nonatomic) IBOutlet UILabel *statusTitleLabel;




@property (weak, nonatomic) IBOutlet QMUIButton *albumButton;
@property (weak, nonatomic) IBOutlet QMUIButton *photographButton;


/** 遮罩 */
@property (weak, nonatomic) IBOutlet CameraHollowView *maskView;
//@property (weak, nonatomic) IBOutlet UIView *clearColorBGView;
/** 图片选取框 */
@property (weak, nonatomic) IBOutlet UIImageView *takePhotoScopeBoxImageView;
/** 拍照提示 */
@property (weak, nonatomic) IBOutlet UILabel *takePhotoTipsLabel;


// 记录变焦
//lastScale lastPoint
@property (nonatomic, assign) CGFloat lastScale;
@property (nonatomic, assign) CGPoint lastPoint;
@end
@implementation CustomCameraShootPreview {
    
}
+ (CustomCameraShootPreview *)instanceWithFrame:(CGRect)frame {
    CustomCameraShootPreview *view = (CustomCameraShootPreview *)[[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] firstObject];
        [view layoutIfNeeded];
    view.frame = frame;
    return view;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    [self layoutIfNeeded];
    CGRect rect = self.takePhotoScopeBoxImageView.frame;
    self.maskView.hollowRect = rect;
    self.clipRect = rect;
}
//- (instancetype)initWithFrame:(CGRect)frame delegate:(id<CustomCameraShootPreviewDelegate>)delegate {
//    if (self = [super initWithFrame:frame]) {
//        self = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil] objectAtIndex:0];
//        NSLog(@"frame- %@",NSStringFromCGRect(frame));
//        self.frame = frame;
//        NSLog(@"self.frame- %@",NSStringFromCGRect(self.frame));
//        _delegate = delegate;
//        
//        self.maskView.hollowRect = self.takePhotoScopeBoxImageView.frame;
//        self.clipRect = self.takePhotoScopeBoxImageView.frame;
//        self.takePhotoTipsLabel.text = takePhotoTipsString1;
//        [self _setupButtonsProperty];
//
//    }
//    return self;
//}
- (void)awakeFromNib {
    [super awakeFromNib];
    
    // 默认获取图片类型
    self.currentGetImageType = CurrentGetImageTypeCamera;
    
    
}


- (IBAction)shootButtonClick:(UIButton *)sender {
    /*
     1 pop
     2 album
     3 takePhoto
     */
    CustomCameraButtonType type = CustomCameraButtonTypePop;
    switch (sender.tag) {
        case 1:{
            type = CustomCameraButtonTypePop;
        }
            break;
        case 2:{
//            [self isImageRecognize:NO];
            self.currentGetImageType = CurrentGetImageTypeAlbum;
            type = CustomCameraButtonTypeAlbum;
        }
            break;
        case 3:{
//            [self isImageRecognize:YES];
            self.currentGetImageType = CurrentGetImageTypeCamera;
            type = CustomCameraButtonTypeTakePhoto;
        }
            break;
//        case 4:{
//            [self isImageRecognize:YES];
//            type = CustomCameraButtonTypeTakePhoto;
//        }
//            break;
        default:
            break;
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(customCameraButtonClickWithType:)]) {
        [self.delegate customCameraButtonClickWithType:type];
    }
}
- (void)isImageRecognize:(BOOL)recognize {
//    self.albumImageView.image = self.albumImage;
    // 是否是图片在识别
    // /显示控件
    if (recognize) {
        [self.photographButton setHidden:YES];
        [self.takePhotoTipsLabel setHidden:NO];
        [self.albumButton setHidden:YES];
        [self.takePhotoScopeBoxImageView setHidden:YES];
        if (self.currentGetImageType == CurrentGetImageTypeAlbum) {
            // 当前是相册获取方式
            [self.albumMaskView setHidden:NO];
        }
    } else {
        [self.photographButton setHidden:NO];
        [self.takePhotoTipsLabel setHidden:YES];
        [self.albumButton setHidden:NO];
        [self.takePhotoScopeBoxImageView setHidden:NO];
        if (self.currentGetImageType == CurrentGetImageTypeAlbum) {
            // 当前是相册获取方式
            [self.albumMaskView setHidden:YES];
        }
    }
}
@end
